import { getPayChannelById, queryPayChannelList } from '../services/platfrom';

/**
 * Created by Adi(adi@imeth.cn) on 2018/9/4.
 */
export class Request {
  effects = {};

  reducer = {};

  constructor(list) {
    this.list = list;
    this.effects = this.getEffects(list);
    this.reducer = this.getReducers(list);
  }

  getEffects(list) {
    let effectsObject = {};

    list.forEach(({ type, requestWay }) => {
      effectsObject[type] = ({ payload = {} }, effects) =>
        this.effectsRequest(`${type}Result`, payload, effects, requestWay);
    });
    return effectsObject;
  }

  getReducers(list) {
    let reducerObject = {};

    list.forEach(({ type }) => {
      reducerObject[`${type}Result`] = (state, action) => {
        return {
          ...state,
          [`${type}Result`]: action.payload,
        };
      };
    });
    return reducerObject;
  }

  *effectsRequest(type, payload = {}, { call, put }, requestWay) {
    yield put({
      type,
      payload: {
        sign: 'doing',
      },
    });
    const response = yield call(requestWay, payload);
    yield put({
      type: type,
      payload: {
        ...response,
        sign: 'done',
      },
    });
  }
}

// export function* effectsRequest(type, payload = {}, { call, put }, requestWay) {
//   yield put({
//     type,
//     payload: {
//       sign: 'doing',
//     },
//   });
//   const response = yield call(requestWay, payload);
//   yield put({
//     type: type,
//     payload: {
//       ...response,
//       sign: 'done',
//     },
//   });
// }
